package com.ztman.rxjava.model.entityExtends.self;

import com.ztman.rxjava.model.baseEntity.BaseResponse;
import com.ztman.rxjava.server.retrofit.OnNetRequestListener;
import com.ztman.rxjava.server.retrofit.RetrofitService;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

/**
 * Created by YJH on 2017/11/29.
 * 功能：网络请求
 * 个人信息的数据请求和数据类型的转换并输出
 */

public class SelfModelIml implements SelfModel{
    @Override
    public void setSelfParams(int accountid, final OnNetRequestListener listener) {
        int accoundId = 2;
        Observable<BaseResponse<SelfInfo>> observable = RetrofitService.getInstance().createSelfApi().getSelfInfo(accoundId);

        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        listener.onRequestStart();
                    }
                }).subscribe(new Subscriber<BaseResponse<SelfInfo>>() {
            @Override
            public void onCompleted() {
                listener.onRequestFinish();
            }

            @Override
            public void onError(Throwable e) {
                listener.onRequestFailure(e);
                listener.onRequestFinish();
            }

            @Override
            public void onNext(BaseResponse<SelfInfo> selfInfoBaseResponse) {
                //成功获取到数据并转换类型输出
                listener.onRequestSuccess(selfInfoBaseResponse);
            }
        });
    }
}
